<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/09/23 01:50:30 $)
// $RCSfile: functions_reports.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


/**
* Load the staff schedule
*/
function getReportStaffSchedule($staffid)
{
	global $dbCore, $_SWIFT;

	if (isset($_SWIFT["staffschedulecache"][$staffid]))
	{
		return $_SWIFT["staffschedulecache"][$staffid];
	}

	$_schedule = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."staffschedules` WHERE `staffid` = '". intval($staffid) ."';");
	$_SWIFT["staffschedulecache"][$staffid] = $_schedule;

	return $_schedule;
}

/**
* Load the Audit Logs for given staff
*/
function loadAuditLogs($staffid, $datefrom, $dateto)
{
	global $dbCore, $_SWIFT;
	
	$_datefrom = mktime(0,0,0,date("m", $datefrom), date("d", $datefrom), date("Y", $datefrom)) - $_SWIFT["timezone"]*60*60;
	$_dateto = mktime(23,59,59,date("m", $dateto), date("d", $dateto), date("Y", $dateto));
	$_logs = array();
	$dbCore->query("SELECT auditlogs.*, tickets.ticketmaskid FROM `". TABLE_PREFIX . "auditlogs` AS auditlogs LEFT JOIN `". TABLE_PREFIX ."tickets` AS tickets ON (auditlogs.ticketid = tickets.ticketid) WHERE (auditlogs.dateline > '". intval($_datefrom) ."' AND auditlogs.dateline < '". intval($_dateto) ."') AND auditlogs.staffid = '". intval($staffid) ."';");
	while ($dbCore->nextRecord())
	{
		$_logs[] = $dbCore->Record;
	}

	return $_logs;
}

/**
* Render the Day Report
*/
function renderDayReport($day, $_logs, $staffid)
{
	global $dbCore, $_SWIFT;

	if (empty($day))
	{
		$day = edate("d-m-Y");
	}

	$daystore = explode("-", $day);

	$dateline = mktime(0,0,0,$daystore[1], $daystore[0], $daystore[2]);

	// Load the staff schedule
	$_schedule = getReportStaffSchedule($staffid);

	// ======= HEADER =======
	$data .= '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborderround"><tr><td class="row1"><span class="smalltext">
	<table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td width="1" align="left">&nbsp;</td>
	<td width="100%" align="center"><span class="ttexttitle">'. $_SWIFT["staffcache"][$staffid]["fullname"] .': '. date("l, d F Y", $dateline) .'</span></td>
	<td width="1" align="right">&nbsp;</td></tr></table><BR />';

	$data .= '<table border="0" cellpadding="0" cellspacing="0" width="100%" class="calborder">';
	$day = getStrDayRep(date("w", $dateline));
	$daycomp = $day."_enabled";

	for ($ii=0; $ii<24; $ii++)
	{
		$formattedhour = sprintf("%02d:00", $ii);
		$tdclass = "calinactivehour";
	
		$hrdateline = mktime($ii,0,0,$daystore[1], $daystore[0], $daystore[2]);

		$opendateline = returnHourDateline($_schedule[$day."_open"], $dateline);
		$closedateline = returnHourDateline($_schedule[$day."_close"], $dateline);


		$_isworkhour = false;
		if ($_schedule[$daycomp] == "1" && $opendateline <= $hrdateline && $closedateline >= $hrdateline)
		{
			$tdclass = "calactivehour";
			$_isworkhour = true;
		}

		$data .= '<tr class="'. $tdclass .'"><td width="60" style="PADDING: 3px;" class="calhrbg"><span class="tabletitle">'. $formattedhour .'</span></td><td width="1" class="calendarsplitter"><img src="'. $_SWIFT["themepath"] .'space.gif" border="0" width="1" height="1" /></td><td width="100%">';
		$data .= renderHorizontalLogBlocks($_logs, $hrdateline, ($hrdateline+3600));
		$data .= '</td></tr>';

		if ($ii != 23)
		{
			$data .= '<tr height="1" class="calendarsplitter"><td colspan="3"><img src="'. $_SWIFT["themepath"] .'space.gif" border="0" width="1" height="1" /></td></tr>';
		}
	}
	$data .= '</table>';

	$data .= '</span></td></tr></table>';

	$data .= '<BR />';

	return $data;
}

/**
* Render the Event Blocks in Horizontal Order
*/
function renderHorizontalLogBlocks($_logs, $starttimeline, $endtimeline)
{
	global $_SWIFT;

	$data .= '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>';
	foreach ($_logs as $key=>$val)
	{
		$datel = edate("d M Y H:i:s", $val["dateline"]);
		$dates = date("d M Y H:i:s", $starttimeline);
		$datee = date("d M Y H:i:s", $endtimeline);

		if ($datel >= $dates && $datel < $datee)
		{
			$data .= '<tr><td align="left" valign="top"><div style="PADDING: 3px;"><table border="0" cellpadding="3" cellspacing="0" width="100%" class="calborder" onMouseOver="javascript:this.className=\'calborderhover\';" onMouseOut="javascript:this.className=\'calborder\';" onClick="javascript:window.location.href=\'index.php?_m=tickets&_a=viewticket&ticketid='. $val["ticketid"] .'\';" style="CURSOR: pointer;">';
			$data .= '<tr><td align="left" valign="top" class="calhrbg"><span class="smalltext">';
			$data .= htmlspecialchars(iif($_SWIFT["settings"]["t_eticketid"]=="seq",$val["ticketid"], $val["ticketmaskid"]).": ".$val["actionmsg"]);
			$data .= '</span></td></tr>';
			$data .= '</table></div></td></tr>';
		}
	}
	$data .= '</table>';

	return $data;
}

/**
* Return Unix timestamp for hour on curent day
*/
function returnHourDateline($firsthr, $dateline)
{
	// We always expect to have a 24 hr format
	$hrtime1 = explode(":", $firsthr);

	$hrtimeresult1 = mktime($hrtime1[0], $hrtime1[1], 0, date("m", $dateline), date("d", $dateline), date("Y", $dateline));

	return $hrtimeresult1;
}

?>